home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 30B (1988-06-08)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 30B (1988-06-08)(Pacific North-West Amigas Club)[WB].adf / DNet1.20 / server / sterm.c < prev   
C/C++ Source or Header  |  1988-03-22  |  4KB  |  180 lines

  1.  
  2. /*
  3.  *  S_TERM.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  Terminal window server.
  8.  *    -Echo what is received in the window.
  9.  *    -Transmit stuff typed on keyboard, echoing locally.
  10.  *
  11.  *  Use FTERM on the other Amiga to connect.
  12.  */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include "servers.h"
  18. #include "/include/typedefs.h"
  19.  
  20. int Enable_Abort;
  21. int NHandlers;
  22.  
  23. long IntuitionBase;
  24. long GfxBase;
  25.  
  26. extern struct MsgPort *DListen();
  27. extern struct MsgPort *DAccept();
  28. extern struct Window *OpenWindow();
  29. extern void *GetMsg(), *CreatePort(), *AllocMem();
  30. extern PROC *FindTask();
  31.  
  32. struct MsgPort *HdPort;
  33. struct MsgPort *LisPort;
  34. short HandShakeSig;
  35.  
  36. _main()
  37. {
  38.     struct Message *msg;
  39.     long mask, pmask, hdmask;
  40.     PROC *proc = FindTask(NULL);
  41.  
  42.     Enable_Abort = 0;
  43.     LisPort= DListen(PORT_IALPHATERM);
  44.     WaitPort(&proc->pr_MsgPort);
  45.     ReplyMsg(GetMsg(&proc->pr_MsgPort));
  46.     if (!LisPort)
  47.     exit(1);
  48.     HdPort = CreatePort(NULL, 0);
  49.     HandShakeSig = AllocSignal(-1);
  50.     pmask = 1 << LisPort->mp_SigBit;
  51.     hdmask= 1 << HdPort->mp_SigBit;
  52.  
  53.     IntuitionBase   = OpenLibrary("intuition.library", 0);
  54.     GfxBase        = OpenLibrary("graphics.library", 0);
  55.     while (mask = Wait(SIGBREAKF_CTRL_C|pmask|hdmask)) {
  56.     if (mask & SIGBREAKF_CTRL_C)
  57.         break;
  58.     if (mask & hdmask) {
  59.         while (msg = GetMsg(HdPort)) {
  60.         --NHandlers;
  61.         FreeMem(msg, sizeof(*msg));
  62.         }
  63.     }
  64.     if (mask & pmask) {
  65.         while (spawn_handler())
  66.         ;
  67.     }
  68.     }
  69.     DUnListen(LisPort);
  70.     while (NHandlers) {
  71.     WaitPort(HdPort);
  72.     msg = GetMsg(HdPort);
  73.     FreeMem(msg, sizeof(*msg));
  74.     --NHandlers;
  75.     }
  76.     DeletePort(HdPort);
  77.     CloseLibrary(IntuitionBase);
  78.     CloseLibrary(GfxBase);
  79. }
  80.  
  81. /*
  82.  *  Spawn a handler to accept the new connection, if any.  Task sends
  83.  *  a message to HdPort when through.
  84.  */
  85.  
  86. spawn_handler()
  87. {
  88.     extern void term_task();
  89.     long oldhan = NHandlers;
  90.  
  91.     CreateTask("Term.channel", 0, term_task, 2048);
  92.     Wait(1 << HandShakeSig);
  93.     return (oldhan != NHandlers);
  94. }
  95.  
  96. static NW Nw = {
  97.     64, 64, 400, 100, -1, -1,
  98.     VANILLAKEY|CLOSEWINDOW,
  99.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE|NOCAREREFRESH,
  100.     NULL, NULL, (unsigned char *)"DNET-Term", NULL, NULL, 32, 32, -1, -1,
  101.     WBENCHSCREEN
  102. };
  103.  
  104. void
  105. term_task()
  106. {
  107.     struct MsgPort *chan;   /*    actually only the front end is a msgport */
  108.     IMESS *im;
  109.     long n;
  110.     long imask, cmask;
  111.     struct Message *msg;
  112.     struct Window *win;
  113.     char notdone = 1;
  114.     char buf[32];
  115.     IOCON ioc;
  116.  
  117.     geta4();
  118.     chan = (struct MsgPort *)DAccept(LisPort);
  119.     if (chan) {
  120.     DQueue(chan, 32);
  121.     ++NHandlers;
  122.     Signal(HdPort->mp_SigTask, 1 << HandShakeSig);
  123.     if (win = OpenWindow(&Nw)) {
  124.         ioc.io_Command = CMD_WRITE;
  125.         ioc.io_Data = (APTR)win;
  126.         ioc.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  127.         ioc.io_Message.mn_ReplyPort = win->UserPort;
  128.         OpenDevice("console.device", 0, &ioc, 0);
  129.         imask = 1 << win->UserPort->mp_SigBit;
  130.         cmask = 1 << chan->mp_SigBit;
  131.         while (notdone) {
  132.         Wait(imask|cmask);
  133.         while (im = (IMESS *)GetMsg(win->UserPort)) {
  134.             switch(im->Class) {
  135.             case VANILLAKEY:
  136.             n = 1;
  137.             buf[0] = im->Code;
  138.             if (buf[0] == 13) {
  139.                 buf[1] = 10;
  140.                 ++n;
  141.             }
  142.             DWrite(chan, buf, n);
  143.             ioc.io_Data = (APTR)buf;
  144.             ioc.io_Length = n;
  145.             DoIO(&ioc);
  146.             break;
  147.             case CLOSEWINDOW:
  148.             notdone = 0;
  149.             break;
  150.             }
  151.             ReplyMsg(im);
  152.         }
  153.         while (n = DNRead(chan, buf, sizeof(buf))) {
  154.             if (n < 0) {
  155.             notdone = 0;
  156.             break;
  157.             }
  158.             DWrite(chan, buf, n);   /* echo back */
  159.             if (buf[0] == 3)        /*  remote ^C -- done   */
  160.             notdone = 0;
  161.             ioc.io_Data = (APTR)buf;
  162.             ioc.io_Length = n;
  163.             DoIO(&ioc);
  164.         }
  165.         }
  166.         CloseDevice(&ioc);
  167.         CloseWindow(win);
  168.     }
  169.     DClose(chan);
  170.     msg = AllocMem(sizeof(*msg), MEMF_PUBLIC);
  171.     Forbid();
  172.     PutMsg(HdPort, msg);
  173.     } else {
  174.     Forbid();
  175.     Signal(HdPort->mp_SigTask, 1 << HandShakeSig);
  176.     }
  177.     RemTask(NULL);
  178. }
  179.  
  180.